package com.shiguiwu.springboot3.search.controller;

import com.shiguiwu.springboot3.search.dto.document.ContentsDTO;
import com.shiguiwu.springboot3.search.enetity.ContentsIndex;
import com.shiguiwu.springboot3.search.repository.ContentsIndexRepository;
import com.shiguiwu.springboot3.util.R;
import com.shiguiwu.springboot3.util.Snowflake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @description: 文档
 * @author: stone
 * @date: Created by 2023/12/17 19:17
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springboot3.search.controller
 */
@RestController
@RequestMapping("/contents")
public class ContentsController {

    @Autowired
    private ContentsIndexRepository contentsIndexRepository;

    @PostMapping("/")
    public R<String> save(@RequestBody ContentsDTO contentsDTO) {
        ContentsIndex c = new ContentsIndex();
        c.setId(contentsDTO.getId());

        c.setContent(contentsDTO.getContent());
        c.setTitle(contentsDTO.getTitle());
        c.setIntro(contentsDTO.getIntro());


        if (null == contentsDTO.getId()) {
            c.setId(Snowflake.snowflakeId());

            c.setCreateTime(new Date());
            c.setCreateId(1);
            c.setCreateName("张三");
            contentsIndexRepository.save(c);
        }
        else {
            contentsIndexRepository.save(c);
        }
        return R.ok("成功");
    }
}
